import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Media, MediaObject} from "@ionic-native/media";
import {File} from "@ionic-native/file";
import {FileTransfer, FileTransferObject} from "@ionic-native/file-transfer";
import {Base64} from "@ionic-native/base64";

/*
  Generated class for the RecordProvider provider.

  See https://angular.io/guide/dependency-injection for more info on provider s
  and Angular DI.
*/
@Injectable()
export class RecordProvider {

  constructor(public http: HttpClient, public media: Media, public file: File, public transfer: FileTransfer, public base64: Base64) {
  }

  record;
  mediaObj;
  startRecord() {

    this.record = this.file.externalRootDirectory + "Record.wav";
    this.mediaObj = this.media.create(this.record);
    this.mediaObj.startRecord();
  }

  stopRecord() {
    this.mediaObj.onSuccess.subscribe(res => {

      // const fileTransfer: FileTransferObject = this.transfer.create();
      //
      // fileTransfer.upload(this.record, 'http://192.168.0.28:8080/sign/VoiceToWord.do ')
      //   .then((data) => {
      //     // success
      //     alert(JSON.stringify(data));
      //   }, (err) => {
      //     // error
      //     alert(JSON.stringify(err))
      //   })

      // this.base64.encodeFile(this.record).then((base64File: string) => {
      //   alert(base64File);
      // }, (err) => {
      //   alert(err);
      // });

      this.file.readAsBinaryString(this.file.externalRootDirectory, 'Record.wav').then(res=> {
        alert(res);
      }, err => {
        alert(JSON.stringify(err));
      })

    })

    this.mediaObj.stopRecord();

  }

}
